package currency;

import java.awt.EventQueue;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class CurrencyMainUI {

    private JFrame frame;
    private JTextField amount;
    private JTextField destValue;
    private String destCountry;
    private final ButtonGroup buttonGroup = new ButtonGroup();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CurrencyMainUI window = new CurrencyMainUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public CurrencyMainUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setTitle("货币转换器");
        frame.setBounds(100, 100, 487, 453);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("人民币金额");
        lblNewLabel.setBounds(43, 64, 88, 25);
        frame.getContentPane().add(lblNewLabel);

        amount = new JTextField();
        amount.setBounds(192, 66, 177, 23);
        frame.getContentPane().add(amount);
        amount.setColumns(10);

        JLabel label = new JLabel("等价于");
        label.setBounds(43, 120, 54, 15);
        frame.getContentPane().add(label);

        destValue = new JTextField();
        destValue.setEditable(false);
        destValue.setBounds(192, 117, 177, 21);
        frame.getContentPane().add(destValue);
        destValue.setColumns(10);

        JButton caculate = new JButton("计算");
        caculate.addActionListener(e -> {
            try {
                Parser parser = new Parser(destCountry, amount.getText());
                parser.parse();

                Converter converter = new Converter(new ExchangeRateServiceImpl());
                double result = converter.convertTo(CurrencyType.RMB, parser.toCurrencyType(), parser.amount());

                destValue.setText(String.valueOf(result));
            } catch (ParseException exception) {
                JOptionPane.showMessageDialog(frame, exception.getMessage(),"消息对话框",JOptionPane.WARNING_MESSAGE);
            }
        });

        caculate.setBounds(318, 189, 93, 23);
        frame.getContentPane().add(caculate);

        JButton clear = new JButton("清除");
        clear.addActionListener(e -> {

            destValue.setText("");
            amount.setText("");

        });
        clear.setBounds(318, 237, 93, 23);
        frame.getContentPane().add(clear);

        JButton exit = new JButton("退出");
        exit.addActionListener(e ->System.exit(0));
        
        exit.setBounds(318, 288, 93, 23);
        frame.getContentPane().add(exit);

        JRadioButton usd = new JRadioButton("美国");
        usd.addActionListener(e -> destCountry = "USD");

        buttonGroup.add(usd);
        usd.setBounds(38, 189, 121, 23);
        frame.getContentPane().add(usd);

        JRadioButton cad = new JRadioButton("加拿大");
        cad.addActionListener(e -> destCountry = "CAD");

        buttonGroup.add(cad);
        cad.setBounds(38, 225, 121, 25);
        frame.getContentPane().add(cad);

        JRadioButton jpy = new JRadioButton("日本");
        jpy.addActionListener(e -> destCountry = "JPY");

        buttonGroup.add(jpy);
        jpy.setBounds(38, 268, 121, 23);
        frame.getContentPane().add(jpy);

        JRadioButton eur = new JRadioButton("欧共体");
        eur.addActionListener(e -> destCountry = "EUR");

        buttonGroup.add(eur);
        eur.setBounds(38, 307, 121, 23);
        frame.getContentPane().add(eur);

    }
}
